package com.pigs.pigscheckinginsystem.controller;


import com.mongodb.client.result.UpdateResult;
import com.pigs.pigscheckinginsystem.entity.CheckingIn;
import com.pigs.pigscheckinginsystem.model.ResultFormat;
import com.pigs.pigscheckinginsystem.service.CheckingInService;
import com.pigs.pigscheckinginsystem.util.ResultUtil;
import com.pigs.pigscheckinginsystem.util.SystemUtils;
import com.pigs.pigscheckinginsystem.util.TimeUtitl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author PIGS
 * @since 2020-04-22
 */
@RestController
@RequestMapping("/home/")
public class CheckingInController {

    @Autowired
    private CheckingInService checkingInService;

    private Logger logger = LoggerFactory.getLogger(CheckingInController.class);

    /**
     * 考勤
     *
     * @param checkingIn
     * @return
     */
    @PostMapping("checkingIn")
    public ResultFormat home(@RequestBody CheckingIn checkingIn, HttpServletRequest request) throws ParseException {

        if (checkingIn.getUserName() != null) {
            logger.info("checkingIn={}", checkingIn);
            CheckingIn checkingInQueryUser = checkingInService.queryUser(checkingIn.getUserName());

            if (checkingInQueryUser != null) {

                logger.info("checkingInQueryUser={}", checkingInQueryUser);

                Long timeHms = TimeUtitl.getTimeHms();
                Long morningCheckingInTime = TimeUtitl.formatSfm(8, 20, 00);
                Long afternoonCheckingInTime = TimeUtitl.formatSfm(17, 30, 00);
                Integer hours = TimeUtitl.getHours();

                checkingIn.setUserIp(SystemUtils.getIpAddress(request));
                checkingIn.setUserName(checkingIn.getUserName());


                if (hours > 6 && hours < 12) {
                    checkingIn.setAttendanceTime("8:20");
                    checkingIn.setPunchInTime(TimeUtitl.dateTime());
                    if (timeHms > morningCheckingInTime) {
                        logger.info("你迟到了哦！");
                        checkingIn.setRemark("迟到");
                        UpdateResult checkingIn1 = checkingInService.punchIn(checkingIn);
                        return ResultUtil.success(100, "你迟到了哦！", checkingIn1);
                    }
                    if (timeHms < morningCheckingInTime) {
                        logger.info("签到成功");
                        UpdateResult checkingIn1 = checkingInService.punchIn(checkingIn);
                        return ResultUtil.success("签到成功", checkingIn1);
                    }
                }

                if (hours > 17 && hours < 24) {
                    checkingIn.setAttendanceTime("17:30");
                    checkingIn.setPunchOutTime(TimeUtitl.dateTime());
                    if (timeHms < afternoonCheckingInTime) {
                        logger.info("早退！");
                        checkingIn.setRemark("早退");
                        UpdateResult updateResult = checkingInService.punchIn(checkingIn);
                        return ResultUtil.success(100, "早退成功！", updateResult);
                    }
                    if (timeHms > afternoonCheckingInTime) {
                        logger.info("签退成功");
                        UpdateResult checkingIn1 = checkingInService.punchIn(checkingIn);
                        return ResultUtil.success("签退成功", checkingIn1);
                    }
                }

                return ResultUtil.success(100, "现在不能打卡哦");
            }else {
                return ResultUtil.success(100, "用户不存在");
            }

        }
        return ResultUtil.error(100, "还没有到打卡时间哦");
    }

    /**
     * 查询 未签到的同学
     */
    @GetMapping("queryNoCheckingIn")
    public ResultFormat queryNoCheckingIn() {
        List<CheckingIn> checkingIns = checkingInService.queryNoCheckingIn();
        if (checkingIns == null) {
            return ResultUtil.error(100, "暂无数据");
        }
        return ResultUtil.success(checkingIns);

    }


    /**
     * 查询 已签到的同学
     */
    @GetMapping("queryCheckingIn")
    public ResultFormat queryCheckingIn() {
        List<CheckingIn> checkingIns = checkingInService.queryCheckingIn();
        if (checkingIns == null) {
            return ResultUtil.error(100, "暂无数据");
        }
        return ResultUtil.success(checkingIns);

    }


}
